package com.yanqu.road.utils;

import com.google.protobuf.AbstractMessage.Builder;
import com.google.protobuf.Message;
import com.yanqu.road.message.YanQuMessage;

public class YanQuMessageUtils {

	public static YanQuMessage buildMessage(int code, Builder<?> messageBuilder) {
		return buildMessage(code, messageBuilder.build());
	}

	public static YanQuMessage buildMessageByte(int code, byte[] data) {
		YanQuMessage yanQuMessage = new YanQuMessage(code, -1);
		yanQuMessage.setBytes(data);
		return yanQuMessage;
	}

	public static YanQuMessage buildMessage(int code, Object object) {
		YanQuMessage yanQuMessage = new YanQuMessage(code, object);
		return yanQuMessage;
	}

	public static YanQuMessage buildMessageByte(int code, long playerId, byte[] data) {
		YanQuMessage yanQuMessage = new YanQuMessage(code, playerId);
		yanQuMessage.setBytes(data);
		return yanQuMessage;
	}

/*	public static YanQuMessage buildMessageByte(int code, long playerId, int byteLength, byte[] data) {
		YanQuMessage yanQuMessage = new YanQuMessage(code, playerId);
		yanQuMessage.setMsgDataLength(byteLength);
		yanQuMessage.setBytes(data);
		return yanQuMessage;
	}*/

	public static YanQuMessage buildMessage(int code, Message message) {
		YanQuMessage yanQuMessage = new YanQuMessage(code, -1);
		yanQuMessage.setMessage(message);
		return yanQuMessage;
	}

	public static YanQuMessage buildMessage(int code, long playerId, Builder<?> messageBuilder) {
		YanQuMessage yanQuMessage = new YanQuMessage(code, playerId);
		yanQuMessage.setMessage(messageBuilder.build());
		return yanQuMessage;
	}

	public static YanQuMessage buildMessage(int code) {
		YanQuMessage yanQuMessage = new YanQuMessage(code);
		return yanQuMessage;
	}

	public static YanQuMessage buildMessage(int code, long playerId) {
		YanQuMessage yanQuMessage = new YanQuMessage(code, playerId);
		return yanQuMessage;
	}

	/**
	 * 消息大小限制
	 */
	public static boolean isLimitPacketSize(YanQuMessage message){
		int limitSize = 1024 * 1024 * 50;
		if(message.getBytes() != null){
			if(message.getBytes().length > limitSize){
				return true;
			}
		}
		if(message.getMessage() != null){
			if(message.getMessage().getSerializedSize() > limitSize){
				return true;
			}
		}

		return false;
	}
}
